const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    labelListValue: '-1',
    labelList: [],
    selectAllLable: [],//选中的标签
    customData: {},//二级自定义类目的内容
    customDialogShow: false,// 自定义标签的弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      selectAllLable: JSON.parse(options.selectAllLable),
      type: options.type
    })
    // 获取所有类目标签
    _this.allCatalogLabel();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 父标签选择改变标签值
   */
  labelCollapseChange: function (event) {
    var _this = this;
    if (String(event.detail).length > 0){
      _this.setData({
        labelListValue: event.detail
      });
    }else{
      _this.setData({
        labelListValue: "-1"
      });
    }
  },

  /**
   * 二级的自定义选中
   */
  customTap: function (e) {
    var _this = this;
    var pindex = e.currentTarget.dataset.pindex;
    var index = e.currentTarget.dataset.index;
    var custom = _this.data.labelList[pindex]["custom" + index];
    var customData = { "pindex": pindex, "index": index, "custom": custom};
    _this.setData({
      customData: customData
    })
    _this.customDialogShow();
  },

  /**
   * 自定义标签的弹窗
   */
  customDialogShow: function(){
    var _this = this;
    var customDialogShow = _this.data.customDialogShow;
    var selectAllLable = _this.data.selectAllLable;
    var text = _this.data.customData.custom.text;
    if (selectAllLable.length >= 3 && !customDialogShow && text.length <= 0) {
      util.dialogAlert("最多只能选三个标签");
      return false;
    }
    _this.setData({
      customDialogShow: !customDialogShow
    })
    if (customDialogShow){
      _this.setData({
        customData: {}
      })
    }
  },

  /**
   * 自定义弹窗的input输入事件
   */
  customInput: function(e){
    var _this = this;
    _this.setData({
      ["customData.custom.text"]: e.detail.value
    })

  },

  /**
   * 自定义标签的弹窗确认事件
   */
  customDialogSuccess: function(){
    var _this = this;
    var customData = _this.data.customData;
    var pindex = customData.pindex;
    var index = customData.index;
    var text = customData.custom.text;
    text = text.trim();
    var selectAllLable = _this.data.selectAllLable;
    var label = _this.data.labelList[pindex];
    var showCheakedText = label.showCheakedText;
    var _i = "";
    for (var i = 0; i < selectAllLable.length; i++) {
      if (index == selectAllLable[i].customIndex && label.id == selectAllLable[i].parentid) {
        _i = i;
      }
    }
    if(text.length > 0){
      if (String(_i).length > 0){
        selectAllLable[_i].name = text;
      } else {
        var j = {};
        j.type = 2;
        j.id = "";
        j.name = text;
        j.parentid = label.id;
        j.parentname = label.name;
        j.customIndex = index;
        selectAllLable.push(j);
      }
      _this.setData({
        selectAllLable: selectAllLable,
        ["labelList[" + pindex + "].custom" + index + ".checked"]: true,
        ["labelList[" + pindex + "].custom" + index + ".text"]: text
      })
    } else {
      console.log(_i);
      if (String(_i).length > 0) {
        selectAllLable.splice(_i, 1);
      }
      _this.setData({
        selectAllLable: selectAllLable,
        ["labelList[" + pindex + "].custom" + index + ".checked"]: false,
        ["labelList[" + pindex + "].custom" + index + ".text"]: ""
      })
    }
    _this.customDialogShow();
  },

  /**
   * 子标签选择
   */
  labelTagTap: function(e) {
    var _this = this;
    var pindex = e.currentTarget.dataset.pindex;
    var index = e.currentTarget.dataset.index;
    var label = _this.data.labelList[pindex];
    var tag = label.tags[index];
    var selectAllLable = _this.data.selectAllLable;
    if (selectAllLable.length >= 3 && tag.checked != true){
      util.dialogAlert("最多只能选三个标签");
      return false;
    }
    if (tag.checked == true) {
      for (var i = 0; i < selectAllLable.length; i++) {
        var l = selectAllLable[i];
        if (l.id == tag.id) {
          selectAllLable.splice(i, 1);
        }
      }
    }else{
      var j = {};
      j.type = 1;
      j.id = tag.id;
      j.name = tag.name;
      j.parentid = label.id;
      j.parentname = label.name;
      j.customIndex = "";
      selectAllLable.push(j);
    }
    var showCheakedText = "";
    for (var i = 0; i < selectAllLable.length; i++) {
      if (selectAllLable[i].type == 1 && selectAllLable[i].parentid == label.id){
        if (showCheakedText.length <= 0) {
          showCheakedText = showCheakedText + selectAllLable[i].name;
        } else {
          showCheakedText = showCheakedText + "、" + selectAllLable[i].name;
        }
      }
    }
    _this.setData({
      selectAllLable: selectAllLable,
      ["labelList[" + pindex + "].showCheakedText"]: showCheakedText,
      ["labelList[" + pindex + "].tags[" + index + "].checked"]: !tag.checked
    })
  },

  /**
   * 确定按钮点击事件
   */
  success: function () {
    var _this = this;
    var selectAllLable = _this.data.selectAllLable;
    if (selectAllLable.length > 0) {
      var selectLableTextArray = [];
      var labelList = _this.data.labelList;
      for (var i = 0; i < labelList.length; i++) {
        var id = labelList[i].id;
        var name = labelList[i].name;
        var nbsp_text = "";
        for (var s = 0; s < name.length; s++) {
          nbsp_text = nbsp_text + "&nbsp;&nbsp;&nbsp;"
        }
        nbsp_text = nbsp_text + "&nbsp;&nbsp;"
        var customParentid = _this.data.customParentid;
        if (id == customParentid) {
          var text1 = "";
          var text2 = "";
          var text3 = "";
          var customText1 = labelList[i].custom1.text;
          var customText2 = labelList[i].custom2.text;
          var customText3 = labelList[i].custom3.text;
          if (customText1.length > 0) {
            text1 = name + "·" + customText1;
          }
          if (customText2.length > 0) {
            if (text1.length > 0) {
              text2 = nbsp_text + customText2;
            } else{
              text1 = name + "·"  + customText2;
            }
          }
          if (customText3.length > 0) {
            if (text2.length > 0) {
              text3 = nbsp_text + customText3;
            } else if (text1.length > 4) {
              text2 = nbsp_text + customText3;
            } else {
              text1 = name + "·" + customText3;
            }
          }
          if (text1.length > 0) {
            selectLableTextArray.push(text1);
          }
          if (text2.length > 0) {
            selectLableTextArray.push(text2);
          }
          if (text3.length > 0) {
            selectLableTextArray.push(text3);
          }
        } else {
          var text1 = "";
          var text2 = "";
          var customText1 = labelList[i].custom1.text;
          var showCheakedText = labelList[i].showCheakedText;
          if (customText1.length > 0 && showCheakedText.length > 0) {
            text1 = name + "·" + showCheakedText;
            text2 = nbsp_text + customText1;
          } else if (customText1.length > 0 || showCheakedText.length > 0) {
            text1 = name + "·" + (customText1.length > 0 ? customText1 : showCheakedText);
          }
          if (text1.length > 0) {
            selectLableTextArray.push(text1);
          }
          if (text2.length > 0) {
            selectLableTextArray.push(text2);
          }
        }
      }

      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.setData({
        selectAllLable: _this.data.selectAllLable,
        selectLableTextArray: selectLableTextArray,
      });
      if (_this.data.type != null && _this.data.type!=''){
        if (_this.data.type =="journeyImg"){
          app.journey.selectAllLable = selectAllLable
          app.journey.selectLableTextArray = selectLableTextArray
        }
      }
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 获取所有类目标签
   */
  allCatalogLabel: function () {
    var _this = this;
    util.post(api.Home_AllCatalogLabelUrl).then(function (res) {
      if (res.code == 200) {
        var list = res.result.personalityLabelList;
        var customParentid = res.result.customParentid;
        _this.setData({
          labelList: list,
          customParentid: customParentid
        })
        var selectAllLable = _this.data.selectAllLable;
        for (var i = 0; i < list.length; i++) {
          var i_id = list[i].id;
          var i_showCheakedText = "";
          var customText1 = "";
          var customText2 = "";
          var customText3 = "";
          for (var j = 0; j < selectAllLable.length; j++) {
            var j_id = selectAllLable[j].id;
            var j_type = selectAllLable[j].type;
            var j_name = selectAllLable[j].name;
            var j_parentid = selectAllLable[j].parentid;
            if (j_parentid != customParentid){
              // 一级类目不是自定义
              if (i_id == j_parentid){
                if (j_type == 1){
                  i_showCheakedText = i_showCheakedText + (i_showCheakedText.length > 0 ? "、" : "") + j_name;
                  _this.setData({
                    ["labelList[" + i + "].showCheakedText"]: i_showCheakedText
                  })
                  for (var x = 0; x < list[i].tags.length; x++) {
                    if (j_id == list[i].tags[x].id){
                      _this.setData({
                        ["labelList[" + i + "].tags[" + x + "].checked"]: true
                      })
                    }
                  }
                } else if (j_type == 2) {
                  customText1 = j_name;
                  _this.setData({
                    ["labelList[" + i + "].custom1.checked"]: true,
                    ["labelList[" + i + "].custom1.text"]: customText1
                  })
                }
              }
            } else if (j_parentid == customParentid && i_id == j_parentid){
              // 一级类目是自定义
              if (customText1.length <= 0) {
                customText1 = j_name;
                _this.setData({
                  ["labelList[" + i + "].custom1.checked"]: true,
                  ["labelList[" + i + "].custom1.text"]: customText1
                })
              } else if (customText2.length <= 0) {
                customText2 = j_name;
                _this.setData({
                  ["labelList[" + i + "].custom2.checked"]: true,
                  ["labelList[" + i + "].custom2.text"]: customText2
                })
              } else if (customText3.length <= 0) {
                customText3 = j_name;
                _this.setData({
                  ["labelList[" + i + "].custom3.checked"]: true,
                  ["labelList[" + i + "].custom3.text"]: customText3
                })
              }
            }
          }
        }
      }
    })
  }
})